import component from "../../../component/Component.js"
import SensorUtils from "../SensorUtils.js";
import { AlarmStatus, LayerType } from "../../../experience/db/Type.js"

export default class SensorRelationAlarm extends component {
    constructor() { super() }

    control() {
        this.sensorUtils = new SensorUtils();
        this.setted_sensor_data = null
        this.emitter.on('sm-alarm-relation',async () => {
            const {node_id:sensor_node_id,denormal_x:dx,denormal_y:dy} = this.emitter.params[0];
            const current_sensor = this.setted_sensor_data.find((sensor) => {
                return sensor.node_id == sensor_node_id
            })
            if(AlarmStatus[current_sensor?.alarm ?? null]) {
                this.sensorUtils.highLightAnimation([dx, dy], { type:'传感器' },3000);
                const relation_broadcasts = this.links[sensor_node_id]?.speakers;
                const relation_videos = this.links[sensor_node_id]?.videos;
                // 将关联的所有关联广播站 都进行报警
                this.broadcastRelationAlarm(relation_broadcasts);
                this.videoRelationAlarm(relation_videos);
            }
        })

        // 接受 从外部传过来的 传感器关联关系的数据
        this.emitter.on('integration-receive',() => {
            this.links = this.emitter.params[0];
        });

        // 把存放在联动报警组件中的 传感器以及监控分站进行一次更新
        this.emitter.on('sensors-infos-update',async () => {
            const {data} = await this.sensorUtils.setted_sensor_list();
            this.setted_sensor_data = data
        });

        // 接受 从外部传过来的 断电区域的数据（里面包括了 关联的断馈电传感器）
        this.emitter.on('powered-areas-receive',() => {
            const areasinfo = this.emitter.params[0];
            console.log('报警里的 areas',areasinfo);
            this.areas = areasinfo;
        });
    }

    template() { }

    // 传感器触发报警时候，关联的广播设备要发生的变化 ,par广播站数组
    broadcastRelationAlarm(speakers) {
        if(speakers) {
            const features = this.drawer.source(LayerType.SMBroadCast).getFeatures();
            const relationSpeakers = features.filter((el) => {
                return speakers.some(item =>el.get('node_id') === item);
            });

            relationSpeakers.forEach((speaker) => {
                const p = speaker.getGeometry().getCoordinates();
                const d = [this.algo.denormalX(p[0]),this.algo.denormalY(p[1])];
                this.sensorUtils.highLightAnimation(d, { type: '广播站' },3000);
            });
        }
    }

    // 传感器触发报警时候，关联的摄像头要发生的变化, par 摄像头数组
    videoRelationAlarm(videos) {
        if(videos) {
            const features = this.drawer.source(LayerType.SMVideo).getFeatures();
            const relationVideos = features.filter((el) => {
                return videos.some(item => el.get('node_id') === item);
            });

            relationVideos.forEach((video) => {
                const p = video.getGeometry().getCoordinates();
                const d = [this.algo.denormalX(p[0]),this.algo.denormalY(p[1])]
                this.sensorUtils.highLightAnimation(d, {type:'摄像头'}, 3000);
            })
        }
    }

    // 传感器触发报警时候，关联的断电区域要发生的变化, par 关联断电区域数组
    PoweredAreaRelationAlarm(node_id) {
        if(!this.areas) { return; }

        const areas = this.areas
            .filter(el => el.nodes === node_id)
            .map(item => item.id);

        const areaFeatures = this.drawer.source(LayerType.SMPoweredArea).getFeatures();
        areaFeatures.forEach((f) => {
            if(areas.some(a => a === f.get('id'))){
                const style = f.getStyle();
                style.setFill(new ol.style.Fill({ color: 'rgba(255, 0, 0, 0.3)' }));
                style.setStroke(new ol.style.Stroke({ color: 'orange' }));
                f.setStyle(style);
            }
        });
    }
}
